package com.mt.lulucai.usbdriver.help;

import com.mt.lulucai.log.LogWriter;

import java.io.IOException;

/**
 * Created by daiguangtao on 2017/6/4.
 * 蓝牙通讯控制类
 * 通过帧形式传播
 * 帧头0xAA 帧尾 0x55 转义 0xA5
 * 转义值： 帧头0xA501 帧尾 0xA502 转义 0xA503
 */

public class MyUsbDriver {
    private int timeOut = 1000; // 蓝牙
    private HexUtil mHexUtil= new HexUtil();//16进制操作类
    LogWriter logWriter = LogWriter.getinstance();
    private GlobalVariable mGlobalVariable;

    public MyUsbDriver() {
        mGlobalVariable = GlobalVariable.getinstance();
    }

    /**
     * 设置超时时间
     */
    public void setTimeOut(int timeOut){
        this.timeOut = timeOut;
    }

    /**
     * 蓝牙通讯 发送蓝牙信息
     * @param sendMsg
     */
    private void sendMsg(String sendMsg){
        StringBuffer sb = new StringBuffer();
        sb.append("AA");
        sb.append("55");
        sb.append(getSendData(sendMsg));
        logWriter.printSendInfo(this.getClass(),"发送数据："+sb.toString());;
        try {
            mGlobalVariable.sendMsg(sb.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取时间的16进制数据
     * @param sendMsg
     * @return String
     */
    private String getSendData(String sendMsg) {
        StringBuffer sb = new StringBuffer();
        byte[] tByte = mHexUtil.hexStringToBytes(sendMsg);
        long l = 0;
        for(byte byteValue:tByte){
            l = l + (byteValue & 0xFF);
            sb.append(escape(byteValue & 0xFF));
        }
        sb.append(escape(l & 0xFF));
        return sb.toString();
    }
    private String escape(long longParam){
        String hexStr = Long.toHexString(longParam).toUpperCase();
        if(hexStr.length() == 1){
            hexStr = "0"+ hexStr;
        }
        return hexStr;
    }
    private String intToHex(int n) {
        StringBuffer s = new StringBuffer();
        String a;
        char []b = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
        while(n != 0){
            s = s.append(b[n%16]);
            n = n/16;
        }
        a = s.reverse().toString();
        return a;
    }

    /**
     * 发送开始标定命令
     * command Id：01
     * 例如:
     * 发送：AA 55 01 10 04 15
     * @return
     */
    public boolean sendStartCalibration(){
        sendAwakenMeasure();
        try {
            //发送开始标定
            Thread.sleep(500);
        }catch  (Exception e){

        }
        StringBuffer sb = new StringBuffer();
        sb.append("01");
        sb.append("10");
        sb.append("04");
        sendMsg(sb.toString());
        return true;
    }
    /**
     * 发送开始测量命令
     * command Id：01
     * 例如:
     * 发送：AA 55 01 72 00 73
     * @return
     */
    public boolean sendAwakenMeasure(){
        StringBuffer sb = new StringBuffer();
        sb.append("01");
        sb.append("72");
        sb.append("00");
        sendMsg(sb.toString());
        return true;
    }

    /**
     * 发送开始测量命令
     * command Id：01
     * 例如:
     * 发送：AA 55 01 10 08 19
     * @return
     */
    public boolean sendStartMeasure(){
        sendAwakenMeasure();
        try {
            //发送开始标定
            Thread.sleep(500);
        }catch  (Exception e){

        }
        StringBuffer sb = new StringBuffer();
        sb.append("01");
        sb.append("10");
        sb.append("08");
        sendMsg(sb.toString());
        return true;
    }
    /**
     * 发送正在测量命令
     * command Id：01
     * 例如:
     * 发送：AA 55 01 13 14 28
     * @return
     */
    public boolean sendStatusMeasure(){
        StringBuffer sb = new StringBuffer();
        sb.append("01");
        sb.append("13");
        sb.append("14");
        sendMsg(sb.toString());
        return true;
    }
    /**
     * 格式化string前补0
     * @param str
     * @param length
     * @return
     */
    private String getFormatStr(String str,int length){
        StringBuilder sb = new StringBuilder();
        while(sb.length()<(length-str.length())){
            sb.append("0");
        }
        sb.append(str);
        return sb.toString();
    }
}
